#include "a9main.h"

#define N 32

#define STO_NO 1

//char status[2][6] = {"Close", "Open"};
//char fan_status[4][6] = {"Close", "One", "Two", "Three"};



int cgiMain()
{
	key_t key;
	int shmid,semid;
	shm_addr_t *shm_buf;
  //  memset(shm_bu/f,0,sizeof(shm_buf));	
	
	if((key = ftok("/app",'g')) <0)
	{
		perror("ftok");
		exit(1);
	}
	printf("key = %x\n",key);
    semid  = semget(key, 1, IPC_CREAT|IPC_EXCL|0666);
	if( semid == -1)
	{
		if(errno == EEXIST)
		{
			semid = semget(key,1,0666);
		}else{
		perror("semget");
		exit(1);
		}
	}
#if 0
	if((shmid = shmget(key, N, 0666 )) == -1)
	{
			perror("shmget");
			exit(1);
	}
#endif
    
	shmid = shmget(key,N,IPC_CREAT|IPC_EXCL|0666);
	if(shmid == -1)
	{
	    if(errno == EEXIST)
		{
		  shmid = shmget(key,0,0);
		}else{
		  perror("shmget errno");
		  exit(1);
		}
	}


	if((shm_buf = (shm_addr_t*)shmat(shmid, NULL, 0)) == (void*)-1 )
	{
		perror("shmat");
		exit(1);
	}
	shm_addr_t cmdbuf;
	sem_p(semid,0);//减1
	memcpy(&cmdbuf, shm_buf, sizeof(shm_addr_t));
	sem_v (semid, 0);

	cgiHeaderContentType("text/html");
	//	fprintf(cgiOut, "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><style type = \"text/css\"><!--body{line-height:50%}--></style></head>");
	///	fprintf(cgiOut, "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><style><!--body{line-height:50%}--></style></head>");
	fprintf(cgiOut, "<head><meta http-equiv=\"refresh\" content=\"1\"><style><!--body{line-height:50%}--></style></head>");
	fprintf(cgiOut, "<HTML>\n");
	fprintf(cgiOut, "<BODY bgcolor=\"#666666\">\n");
	fprintf(cgiOut, "<h1><font color=\"#FF0000\">Storage #%d:</font></H2>\n ", STO_NO);

	fprintf(cgiOut, "<h2><font face=\"Broadway\"><font color=\"#FFFAF0\">Real-time information environment</font></font></H2>\n ");	
	fprintf(cgiOut, "<h4>温度:\t%d.%d </H4>\n ", cmdbuf.temp[0], cmdbuf.temp[1]);
	fprintf(cgiOut, "<h4>湿度:\t%d.%d </H4>\n ", cmdbuf.hum[0],cmdbuf.hum[1]);
	fprintf(cgiOut, "<h4>光照:\t%d</H4>\n ", cmdbuf.light);
	//fprintf(cgiOut, "<h4>Battery:\t%0.2f</H4>\n ", shm_buf->rt_status.storage_no[STO_NO].battery);
	//fprintf(cgiOut, "<h4>Adc:\t%0.2f</H4>\n ", shm_buf->rt_status.storage_no[STO_NO].adc);
	fprintf(cgiOut, "<h4>X:\t%d</H4>\n ", cmdbuf.x);
	fprintf(cgiOut, "<h4>Y:\t%d</H4>\n ", cmdbuf.y);
	fprintf(cgiOut, "<h4>Z:\t%d</H4>\n ", cmdbuf.z);
	//fprintf(cgiOut, "<h4>Buzzer:\t%s</H4>\n ", status[shm_buf->rt_status.storage_no[STO_NO].buzzer_status]);
	//fprintf(cgiOut, "<h4>Led:\t%s</H4>\n ", status[shm_buf->rt_status.storage_no[STO_NO].led_status]);
	//fprintf(cgiOut, "<h4>Fan:\t%s</H4>\n ", fan_status[shm_buf->rt_status.storage_no[STO_NO].fan_status]);
	//fprintf(cgiOut, "<h4>Temperature\nMAX: %0.0f MIN: %0.0f</H4>\n ", shm_buf->rt_status.storage_no[STO_NO].temperatureMAX, shm_buf->rt_status.storage_no[STO_NO].temperatureMIN);
	//fprintf(cgiOut, "<h4>Humidity\nMAX: %0.0f MIN: %0.0f</H4>\n ", shm_buf->rt_status.storage_no[STO_NO].humidityMAX, shm_buf->rt_status.storage_no[STO_NO].humidityMIN);
	//fprintf(cgiOut, "<h4>Illumination\nMAX: %0.0f MIN: %0.0f</H4>\n ", shm_buf->rt_status.storage_no[STO_NO].illuminationMAX, shm_buf->rt_status.storage_no[STO_NO].illuminationMIN);




	//fprintf(cgiOut, "<h2><font face=\"Broadway\"><font color=\"#FFFAF0\">Close!</font></font></H2>\n ");	

	//fprintf(cgiOut, "<h3>:</H3>\n ");	
	fprintf(cgiOut, "</BODY></HTML>\n");	
	return 0;
}
